<template>
  <div>
    <el-card>
      <template #header class="header">
        <span>工作日历</span>
      </template>
     <div class="select">
          <el-select v-model="year" @change="changeEvent" size="small">
        <el-option
          v-for="item in yearArr"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
      <el-select v-model="month" @change="changeEvent" size="small">
        <el-option
          v-for="item in monthArr"
          :key="item"
          :label="item"
          :value="item"
        >
        </el-option>
      </el-select>
     </div>
      <el-calendar v-model="value">
        <template #dateCell="{data,date}">
          <!-- //子组件变化了父组件也要一起变化 -->
          <div @click="cellClick(date)" style="width:100%;height:100%">
            <span>{{ data.day.split('-')[2] }}</span>
            <span
              class="colorPis"
              v-if="formatDate(date) && data.type === 'current-month'"
              >休</span
            >
          </div>
        </template>
      </el-calendar>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Calendar',
  props: {
    valueTime: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  model: {
    prop: 'valueTime'
  },
  data () {
    return {
      value: new Date(),
      year: '',
      yearArr: [2000, 2001],
      month: '',
      monthArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    }
  },
  watch: {
    valueTime: {
      deep: true, //监听堆和站的变化
      immediate: true, //第一次绑定就立马执行回调函数
      handler (newVal) {
        this.value = newVal
        this.year = newVal.getFullYear()
        this.month = newVal.getMonth() + 1
        //年的时间范围，传入年的前后五年
        const min = this.year - 5
        const max = this.year + 5
        let arr = []
        for (let i = min; i <= max; i++) {
          arr.push(i)
        }
        this.yearArr = arr
      }
    }
  },
  methods: {
    formatDate (time) {
      if (time.getDay() === 0 || time.getDay() === 6) {
        return true
      } else {
        return false
      }
    },
    //子组件变化了父组件也要一起变化
    changeEvent () {
      this.$emit('input', new Date(this.year + '-' + this.month + '-01'))
    },
    cellClick (date) {
      this.$emit('input', date)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .current {
  text-align: center;
  line-height: 70px;
}
.colorPis {
  background-color: orangered;
  color: #fff;
  padding: 5px;
  margin-left: 5px;
  border-radius: 50%;
}
.select{
    text-align: right;
}
.header {
    span {
      color: #2c3e50;
      font-size: 24px;
    }
    .item {
      color: #97a8be;
      float: right;
      padding: 3px 0;
    }
  }
</style>
